//
//  ShoperCarHeaderView.m
//  EasyToTesco2.0
//
//  Created by knew on 2017/3/29.
//  Copyright © 2017年 EasyToTesco. All rights reserved.
//

#import "ShoperCarHeaderView.h"

@implementation ShoperCarHeaderView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self addShopCarHeaderView];
    }
    return self;
}

- (void)addShopCarHeaderView {
    self.leftButton = [[UIButton alloc] init];
    
    [self.leftButton setImage:[UIImage imageNamed:@"gouwuche_2"] forState:UIControlStateNormal];
    [self.leftButton setImage:[UIImage imageNamed:@"gouwuche_0"] forState:UIControlStateSelected];
    [self.leftButton addTarget:self action:@selector(didClickShopCarButton:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.leftButton];
    self.leftButton.selected = self.isSelected;
    
    
    [self.leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left);
        make.top.equalTo(self.mas_top);
        make.bottom.equalTo(self.mas_bottom);
        make.width.mas_equalTo(26);
    }];
    
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tabar_notselected2"]];
    imageView.contentMode = UIViewContentModeCenter;
    [self addSubview:imageView];
    
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.leftButton.mas_right);
        make.top.equalTo(self.mas_top);
        make.width.mas_equalTo(self.mas_height);
        make.height.mas_equalTo(self.mas_height);
    }];
    
    UIButton *rightButton = [[UIButton alloc] init];
    [rightButton setTitle:@"编辑" forState:UIControlStateNormal];
    [rightButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(didClickRightButton:) forControlEvents:UIControlEventTouchUpInside];
    self.rightButton = rightButton;
    [self addSubview:rightButton];
    
    [rightButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.mas_right).offset(-8);
        make.top.equalTo(self.mas_top);
        make.width.mas_equalTo(40);
        make.height.mas_equalTo(40);
    }];
    
    self.titleLabel = [[UILabel alloc] init];
    self.titleLabel.textAlignment = NSTextAlignmentLeft;
    [self addSubview:self.titleLabel];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(imageView.mas_right);
        make.right.equalTo(rightButton.mas_left).offset(-4);
        make.top.equalTo(self.mas_top);
        make.bottom.equalTo(self.mas_bottom);
    }];
}

- (void)didClickRightButton:(UIButton *)button {
    if ([self.delegate respondsToSelector:@selector(didClickShopCarHeaderViewRightButton:)]) {
        [self.delegate didClickShopCarHeaderViewRightButton:button];
    }
}

- (void)didClickShopCarButton:(UIButton *)button {
    button.selected = !button.selected;
    if (self.cartBlock) {
        self.cartBlock(button.selected);
    }

    if ([self.delegate respondsToSelector:@selector(didClickShopCarHeaderViewButton:)]) {
        [self.delegate didClickShopCarHeaderViewButton:button];
    }
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    if ([self.delegate respondsToSelector:@selector(didClickShopCarHeaderView)]) {
        [self.delegate didClickShopCarHeaderView];
    }
}

@end
